from google.appengine.ext import db

from gaeo.controller import BaseController

from model.building import Building

class BuildingController(BaseController):
    def create(self):
        r = Building(
            name = self.params.get('name', None),
            tel = self.params.get('tel', None),
            address = self.params.get('address', None),
            area = self.params.get('area', None),
            sport = self.params.get('sport', None),
        )
        r.put()
        self.redirect('/building')

    def destroy(self):
        r = Building.get(self.params.get('key'))
        if r is not None:
            r.delete()
        self.redirect('/building')

    def edit(self):
        rec = Building.get(self.params.get('key'))
        if rec:
            setattr(self, 'name', rec.name)
            setattr(self, 'tel', rec.tel)
            setattr(self, 'address', rec.address)
            setattr(self, 'area', rec.area)
            setattr(self, 'sport', rec.sport)
        else:
            setattr(self, 'name', 'Error')
            setattr(self, 'tel', 'Error')
            setattr(self, 'address', 'Error')
            setattr(self, 'area', 'Error')
            setattr(self, 'sport', 'Error')
            
        self.flagSelected()

    def index(self):
        query = Building.all()
        self.result = query
        
        self.flagSelected()

    def new(self):
        self.flagSelected()

    def show(self):
        rec = Building.get(self.params.get('key'))
        if rec:
            setattr(self, 'name', rec.name)
            setattr(self, 'tel', rec.tel)
            setattr(self, 'address', rec.address)
            setattr(self, 'area', rec.area)
            setattr(self, 'sport', rec.sport)
        else:
            setattr(self, 'name', 'Error')
            setattr(self, 'tel', 'Error')
            setattr(self, 'address', 'Error')
            setattr(self, 'area', 'Error')
            setattr(self, 'sport', 'Error')
            
        self.flagSelected()

    def update(self):
        r = Building.get(self.params.get('key'))
        r.name = self.params.get('name', None)
        r.tel = self.params.get('tel', None)
        r.address = self.params.get('address', None)
        r.area = self.params.get('area', None)
        r.sport = self.params.get('sport', None)
        r.put()
        self.redirect('/building')
        
     # ----- Not Action --------------------------   
    def flagSelected(self):
        self.selected = 'building'
